package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.mapper.OrderMapper;
import com.itcast.pojo.Member;
import com.itcast.pojo.Order;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrderService;
import com.itcast.vo.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceimp implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Result check(OrderInfo orderInfo) {

        Date orderDate = orderInfo.getOrderDate();
        String idCard = orderInfo.getIdCard();
        String name = orderInfo.getName();
        Integer sex = orderInfo.getSex();
        String telephone = orderInfo.getTelephone();
        Integer setmealId = orderInfo.getSetmealId();
        Integer memberId = orderInfo.getMemberId();
        // 查询用户是否存在不存在就添加
        Member member = orderMapper.findMemberByTelephone(memberId);
        if (member == null) {
            member = new Member();
            member.setIdCard(idCard);
            member.setSex(String.valueOf(sex));
            member.setName(name);
            member.setPhoneNumber(String.valueOf(telephone));
            member.setRegTime(orderDate);
            orderMapper.addMember(member);
        }
       // Member member1 = orderMapper.findMemberByTelephone(telephone);

        //查询数据库看预约的时间有没有档期
        OrderSetting orderSetting = orderMapper.findOrderSettingByDate(orderDate);
        if (orderSetting == null) {
            return Result.error("没有档期");
        }
        //判断下预约是否已满
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        } else {

            //查询订单表 看是否有重复预约 通过会员id 和 套餐 id 和 日期
            Order order = orderMapper.findOrder(member.getId(),setmealId,orderDate);
            if (order == null) {
                //添加订单表
                Order order1 = new Order();
                order1.setOrderDate(orderDate);
                order1.setOrderType(Order.ORDERTYPE_WEIXIN);
                order1.setSetmealId(setmealId);
                order1.setOrderStatus(Order.ORDERSTATUS_NO);
                order1.setMemberId(member.getId());
                orderMapper.addOrder(order1);
                //添加订单就得修改预约表
                orderMapper.updateOrder(orderDate);
                return new Result(true, MessageConstant.ORDER_SUCCESS,member.getId());
            }
            return Result.error("不要重复预约");
        }


    }

    @Override
    public List<Member> findFamily(Integer id) {
        //直接调用mapper层查询相关的人物
       return orderMapper.findFamily(id);


    }

    @Override
    public Result findOrderSuccessByid(Integer id) {
        //id 是member的id
        //查询 t_member 表 获取 name
       String name =orderMapper.findNameById(id);
        //查询 t_order 表 获取 体检日期 和 预约类型
        OrderInfo orderInfo=orderMapper.findOrderByMemberId(id);
        //查询 t_setmeal 表获取 体检套餐
        String setmeal=orderMapper.findSetmealBySetmealId(id);
        orderInfo.setMember(name);
        orderInfo.setSetmeal(setmeal);
        return Result.success(MessageConstant.ORDER_SUCCESS,orderInfo);
    }
}
